package com.shsxt.crm.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shsxt.common.util.JsonUtil;
import com.shsxt.common.util.Result;
import com.shsxt.common.util.StringUtil;
import com.shsxt.common.util.VerificationLoginUtil;
import com.shsxt.crm.dao.YgCustomerServeDao;
import com.shsxt.crm.dao.YgDataDicDao;
import com.shsxt.crm.model.YgCustomerServe;
import com.shsxt.crm.model.YgDataDic;
import com.shsxt.crm.model.YgUser;
import com.shsxt.crm.service.IServerManageService;

@Service
public class ServerManageServiceImpl implements IServerManageService {

	@Autowired
	private YgCustomerServeDao ygCustomerServeDao;

	@Autowired
	private YgDataDicDao ygDataDicDao;

	@Autowired
	private HttpServletRequest request;

	@Override
	public List<YgDataDic> queryDataDicValueByDataDicName(String dataDicName) {
		if (StringUtil.isNotEmpty(dataDicName)) {
			return ygDataDicDao.queryDataDicValueByDataDicName(dataDicName);
		}
		return null;
	}

	@Override
	@Transactional
	public Result insertCustomerServerCreate(YgCustomerServe ygCustomerServe) {
		ygCustomerServe.setState("1");
		ygCustomerServe.setCreateDate(new Date());
		ygCustomerServe.setUpdateDate(new Date());
		ygCustomerServe.setIsValid(1);

		String json = VerificationLoginUtil.checkLoginUser(request);
		YgUser user = JsonUtil.parseJsonWithGson(json, YgUser.class);
		ygCustomerServe.setCreatePeople(user.getUserName());

		long saveSte = ygCustomerServeDao.saveSte(ygCustomerServe);
		if (saveSte == 1) {
			return Result.success("服务创建成功");
		}
		return Result.fail("服务创建失败");
	}

	@Override
	public Map<String, Object> queryCustomerServesByParams(Integer state, Integer pageNum, Integer pageSize) {

		PageHelper.startPage(pageNum, pageSize);

		Map<String, Object> params = new HashMap<>();
		params.put("state", state);
		params.put("isValid", 1);
		List<YgCustomerServe> list = ygCustomerServeDao.find(params);
		PageInfo<YgCustomerServe> info = new PageInfo<>(list);

		Map<String, Object> map = new HashMap<>();

		map.put("total", info.getTotal());
		map.put("rows", info.getList());

		return map;
	}

	@Override
	public Result updateCustomerServes(YgCustomerServe ygCustomerServe) {
		ygCustomerServe.setUpdateDate(new Date());
		ygCustomerServe.setAssignTime(new Date());
		long ste = ygCustomerServeDao.updateSte(ygCustomerServe);
		if (ste == 1) {
			return Result.success("分配成功");
		}
		return Result.fail("分配失败");
	}

}
